package services

import (
	"gitee.com/zhoulvvv/my_go_gateway/dao"
	"gitee.com/zhoulvvv/my_go_gateway/dto"
	"gitee.com/zhoulvvv/my_go_gateway/public"
	"github.com/e421083458/golang_common/lib"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
)

type AdminLoginService struct {
}

func (as *AdminLoginService) LoginCheck(c *gin.Context, params *dto.AdminLoginInput) (*dao.Admin, error) {
	admin := &dao.Admin{UserName: params.UserName, IsDelete: 0}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		return nil, errors.New("数据库连接池获取失败")
	}
	admin, err = admin.Find(c, tx, admin)
	if err != nil {
		return nil, errors.New("用户信息不存在")
	}
	// 获取加密的密码，使用前端传递的密码+数据库查询处理的盐值，进行加密，就得到的了加密的密码
	saltPass := public.GenSaltPassword(admin.Salt, params.Password)
	// 使用加密和数据库存储的加密密码进行比较
	if saltPass != admin.Password {
		return nil, errors.New("密码错误，请重新输入")
	}
	return admin, err
}
